<?php
/**

 //  DoYouHaoBaby-X Ucenter ( DoYouHaoBaby-X 计划 )
 //  +---------------------------------------------------------------------
 //
 //  “Copyright”
 //  +---------------------------------------------------------------------
 //	 | (C) 2010 - 2011 http://doyouhaobaby.net All rights reserved.
 //	 | This is not a free software, use is subject to license terms
 //  +---------------------------------------------------------------------
 //
 //  “About This File”
 //  +---------------------------------------------------------------------
 //	 | Ucenter 配置处理控制器
 //  +---------------------------------------------------------------------

*/

!defined( 'DYHB_PATH' ) && exit;

class OptionController extends InitController{

   /**
    * 获取系统配置
    */
   public function index(){

        // 获取配置数据
		$arrOptionData = &$this->_arrOptions;

		// 时间相关特殊处理
        $arrOptionData['timeoffset'] = intval( $arrOptionData['timeoffset'] / 3600 );
		$arrCheckarray = array( $arrOptionData['timeoffset'] => 'selected="selected"' );
        $this->assign( 'arrCheckarray',$arrCheckarray );

        $arrOptionData['timeformat'] = $arrOptionData ['timeformat'] == 'H:i' ? 1 : 0;
		$arrOptionData['dateformat'] = str_replace( array('y', 'n', 'j'), array('yyyy', 'mm', 'dd'), $arrOptionData['dateformat']);

        // 邮件处理
		$arrItems = array('maildefault', 'mailsend', 'mailserver', 'mailport', 'mailauth', 'mailfrom', 'mailauth_username', 'mailauth_password', 'maildelimiter', 'mailusername', 'mailsilent');

		foreach($arrItems as $sItem ) {

			$arrOptionData[$sItem]  = htmlspecialchars ( $arrOptionData[ $sItem ] );
		}

		$this->assign( 'arrOptions',$arrOptionData );

		$this->display();
   }

   /**
    * 修改配置
    */
   public function update_config(){

       $arrOption = Safe::getGpc( 'configs','P' );

	   foreach( $arrOption as $sKey => $val ){

	   	   // 整理空格
		   $val = trim( $val );

		   if( $sKey =='pmlimit1day'
		       or $sKey == 'pmfloodctrl'
		       or $sKey == 'pmsendregdays'
		       or $sKey == 'admineverynum' ){

		       $val = intval( $val );
		   }

		   if( $sKey == 'timeoffset' ){

		   		$val = in_array( $val, array('-12', '-11', '-10', '-9', '-8', '-7', '-6', '-5', '-4', '-3.5', '-3', '-2', '-1', '0', '1', '2', '3', '3.5', '4', '4.5', '5', '5.5', '5.75', '6', '6.5', '7', '8', '9', '9.5', '10', '11', '12')) ? $val : 8;
           }

		    if( $sKey == 'timeoffset' ){

		       $val = $val * 3600;
		   }

		   if( $sKey == 'dateformat' ){

		       $val = str_replace( array( 'yyyy', 'mm', 'dd' ), array( 'y', 'n', 'j' ), strtolower( $val ) );
		   }

		   if( $sKey == 'timeformat' ){

		       $val = $val == 1 ? 'H:i' : 'h:i A';
		   }

	       // 取得模型
           $oOptionModel  = OptionModel::F( 'option_name=?',$sKey )->getOne();

           $oOptionModel->option_value = $val;

           $oOptionModel->save( 'update' );

           // 开始捕获模型中的错误
           if( $oOptionModel->getErrorMessage( )!='zero-effect' and $oOptionModel->isError( ) ) {

                $this->E( $oOptionModel->getErrorMessage( ) );
           }

	   }

	   $this->S( G::L( '配置文件更新成功了！' ) );
   }

}
